﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Runtime.InteropServices;
using System.Configuration;
using System.Text;
using System.IO;

namespace Commons.Web
{
    using System.Web.Mvc;
    using VCommons;
    using global::Commons.Entity;
    /// <summary>
    /// 前台Controller的基类，
    /// 提供一些共同属性和方法
    /// Created By : 张占岭
    /// Created Date : 2011-10-17
    /// Modify By:
    /// Modify Date:
    /// Modify Reason:
    /// </summary>
    public abstract class BaseController : Controller
    {
        public BaseController()
        {
            this.PageSize = 10;
            this.Current_UserID = SessionAction.ReadSession("UserID");
        }

        #region 属性

        /// <summary>
        /// 当前的用户ID
        /// </summary>
        protected virtual string Current_UserID { get; set; }

        /// <summary>
        /// 当前登陆的用户名
        /// </summary>
        protected virtual string Current_UserName { get; set; }

        /// <summary>
        /// 每页显示的记录数 
        /// </summary>
        protected virtual int PageSize { get; set; }

        /// <summary>
        /// 母板页
        /// </summary>
        protected virtual string MasterFile { get; set; }

        /// <summary>
        /// 通用条件构造器
        /// </summary>
        protected VPredication VP { get; set; }

        /// <summary>
        /// 通用分页参数构造器
        /// </summary>
        protected PageParameters PP { get; set; }

        /// <summary>
        ///通用消息返回类
        /// </summary>
        protected VMessage VM { get; set; }


        #endregion 属性

        #region 方法重写

        /// <summary>
        /// 重写controller的View方法，使它可以根据用户角色应用指定视图
        /// </summary>
        protected new ViewResult View(string viewName, object model)
        {
            return this.View(viewName, model, this.MasterFile);
        }

        /// <summary>
        /// 重写controller的View方法，使用指定母板
        /// </summary>
        /// <param name="viewName"></param>
        /// <param name="model"></param>
        /// <param name="masterFile"></param>
        /// <returns></returns>
        protected new ViewResult View(string viewName, object model, string masterFile)
        {
            ViewData.Model = model;
            return new ViewResult
            {
                ViewName = viewName,
                MasterName = masterFile,
                ViewData = ViewData,
                TempData = TempData
            };
        }

        /// <summary>
        /// 返回默认的视图
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        protected new ViewResult View(object model)
        {
            return this.View(null, model);
        }

        /// <summary>
        /// 返回默认空视图
        /// </summary>
        /// <returns></returns>
        protected new ViewResult View()
        {
            return View(null, null);
        }

        /// <summary>
        /// 操作后，显示的提示成功或失败的信息
        /// author:zzl
        /// </summary>
        /// <param name="msg">提示信息，操作成功或操作失败</param>
        /// <param name="url">点击返回列表后，所进入的地址</param>
        /// <returns></returns>
        protected virtual string DisplayMsg(string msg, string url)
        {
            string message = string.Format("<div style='color:red;font-size:14px;background:rgb(244,250,251);border:rgb(187,221,229) 1px solid;margin:3px;padding:3px'>{0},<a href='{1}'>返回列表</a></div>", msg, url);
            return message;
        }

        protected virtual string DisplayMsg(string msg)
        {
            string message = string.Format("<div style='color:red;font-size:14px;background:rgb(244,250,251);border:rgb(187,221,229) 1px solid;margin:3px;padding:3px'>{0}</div>", msg);
            return message;
        }

        #region 返回JS提示框的View
        protected virtual ViewResult AlertToUrl(string actionName)
        {
            return AlertToUrl("操作成功", actionName, this.ControllerContext.RouteData.GetRequiredString("controller"), null);
        }
        protected virtual ViewResult AlertToUrl(string msg, string actionName)
        {
            return AlertToUrl(msg, actionName, this.ControllerContext.RouteData.GetRequiredString("controller"), null);
        }

        protected virtual ViewResult AlertToUrl(string msg, string actionName, string controllerName)
        {
            return AlertToUrl(msg, actionName, controllerName, null);
        }

        protected virtual ViewResult AlertToUrl(string msg, string actionName, string controllerName, object roteValues)
        {
            Response.Write("<html><head><style type='text/css'>*{font-size:12px;}</style><title></title></head><body><div></div></body> <script src=\"/Scripts/jquery-1.4.1.min.js\" type=\"text/javascript\"></script><script src=\"/Scripts/artDialog4.1.1/jquery.artDialog.js?skin=blue\" type=\"text/javascript\"></script><script src=\"/Scripts/artDialog4.1.1/artDialog.iframeTools.js\" type=\"text/javascript\"></script><script src=\"/Scripts/artJs.js\" type=\"text/javascript\"></script>");
            string url = Url.Action(actionName, controllerName, roteValues);
            Response.Write("<script type=\"text/javascript\" >alertJs('" + msg + "',1,'" + url + "');</script></html>");

            Response.Flush();
            Response.End();
            return null;
        }
        #endregion

        #endregion 方法重写

        #region 返回JS提示信息并跳转ViewResult

        /// <summary>
        /// 返回JS提示信息并跳转ViewResult
        /// </summary>
        /// <remarks>
        /// 提示显示图标默认显示成功,不自动关闭
        /// </remarks>
        /// <param name="msg">提示消息</param>
        /// <param name="actionName">操作的名称</param>
        /// <returns></returns>
        protected ActionResult RedirectArtDialogToAction(string msg, string actionName)
        {
            return RedirectArtDialogToAction(msg, actionName, this.ControllerContext.RouteData.GetRequiredString("controller"), null, MsgStatus.succeed);
        }

        /// <summary>
        /// 返回JS提示信息并跳转ViewResult
        /// </summary>
        ///  <remarks>
        /// 提示显示图标默认显示成功,不自动关闭
        /// </remarks>
        /// <param name="msg">提示消息</param>
        /// <param name="actionName">操作的名称</param>
        /// <param name="controllerName">控制器的名称</param>
        /// <returns></returns>
        protected ActionResult RedirectArtDialogToAction(string msg, string actionName, string controllerName)
        {
            return RedirectArtDialogToAction(msg, actionName, controllerName, null, MsgStatus.succeed);
        }

        /// <summary>
        /// 返回JS提示信息并跳转ViewResult
        /// </summary>
        ///  <remarks>
        /// 提示显示图标默认显示成功,不自动关闭
        /// </remarks>
        /// <param name="msg">提示消息</param>
        /// <param name="actionName">操作的名称</param>
        /// <param name="controllerName">控制器的名称</param>
        /// <param name="msgStatus">提示显示图标（默认显示成功）</param>
        /// <returns></returns>
        protected ActionResult RedirectArtDialogToAction(string msg, string actionName, string controllerName, MsgStatus msgStatus)
        {
            return RedirectArtDialogToAction(msg, actionName, controllerName, null, msgStatus);
        }

        /// <summary>
        /// 返回JS提示信息并跳转ViewResult
        /// </summary>
        ///  <remarks>
        /// 提示显示图标默认显示成功,不自动关闭
        /// </remarks>
        /// <param name="msg">提示消息</param>
        /// <param name="actionName">操作的名称</param>
        /// <param name="controllerName">控制器的名称</param>
        /// <param name="roteValues">路由的参数</param>
        /// <returns></returns>
        protected ActionResult RedirectArtDialogToAction(string msg, string actionName, string controllerName, object roteValues)
        {
            return RedirectArtDialogToAction(msg, actionName, controllerName, roteValues, MsgStatus.succeed);
        }
        /// <summary>
        /// 返回JS提示信息并跳转ViewResult
        /// </summary>
        ///  <remarks>
        /// 提示显示图标默认显示成功,不自动关闭
        /// </remarks>
        /// <param name="msg">提示消息</param>
        /// <param name="actionName">操作的名称</param>
        /// <param name="controllerName">控制器的名称</param>
        /// <param name="roteValues">路由的参数</param>
        /// <param name="msgStatus">提示显示图标（默认显示成功）</param>
        /// <param name="autoClose">是否自动关闭，1：是，0：否</param>
        /// <returns></returns>
        protected ActionResult RedirectArtDialogToAction(string msg, string actionName, string controllerName, object roteValues, MsgStatus msgStatus)
        {
            string url = Url.Action(actionName, controllerName, roteValues);

            string _RedirectJSString = @"
                <link href='/Scripts/jquery-plugin-boxy/css/boxy.css' rel='stylesheet' type='text/css' /> 
                <script src='/js/jquery-1.8.2.min.js' type='text/javascript'></script>
                <script src='/Scripts/jquery-plugin-boxy/js/jquery.boxy.js' type='text/javascript'></script>
                <script src='/Scripts/jquery-plugin-boxy/boxyFun.js' type='text/javascript'></script>

                <script type='text/javascript'>
               try{ $(function(){
                        boxy_HrefFun('" + msg + "','" + url + "');});}catch(e){alert('" + msg + "');location.href='" + url + "';}</script>";

            Response.Write(_RedirectJSString);
            return null;
        }
        protected ActionResult RedirectArtDialogToUrl(string msg, string url)
        {
            return RedirectArtDialogToUrl(msg, MsgStatus.succeed, url);
        }
        protected ActionResult RedirectArtDialogToUrl(string msg, MsgStatus msgStatus, string url)
        {
            string _RedirectJSString = "<html><head><title></title></head><body><div></div></body> <script src=\"/js/jquery-1.8.2.min.js\" type=\"text/javascript\"></script><script src=\"/Scripts/artDialog4.1.1/jquery.artDialog.js?skin=blue\" type=\"text/javascript\"></script><script type=\"text/javascript\" >$(function(){art.dialog({time:2,content:'" + msg + "',icon:'" + msgStatus + "', opacity: 0.7,lock:true}); setTimeout('location.href=\"" + url + "\"',1000);});</script></html>";
            return null;
        }

        /// <summary>
        /// 弹出提示图标
        /// </summary>
        public enum MsgStatus
        {
            /// <summary>
            /// 错误
            /// </summary>
            error = 0,

            /// <summary>
            /// 成功
            /// </summary>
            succeed = 1,

            /// <summary>
            /// 警告
            /// </summary>
            warning = 2,

            /// <summary>
            /// 疑问
            /// </summary>
            question = 3,

            /// <summary>
            /// 笑脸
            /// </summary>
            smile = 4,

            /// <summary>
            /// 悲剧了
            /// </summary>
            sad = 5
        }

        #endregion
    }
}